package com.ymttest.database.mongo.jproduct;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mongodb.WriteResult;
import com.ymt.database.MongoDBDriver;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.YMTDateUtil;

public class LiveProductsWapper {
	private MongoDBDriver mdd = YmtMongoDriverFactroy.getInstance_YmtProductDBdriverNewJava("LiveProducts");
	
	
	public Map<String,Object> getByProductId(String productId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	public Map<String,Object> getByProductIdAndLiveId(String productId, int lid) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\",\"lid\":" + lid + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	
	public Map<String,Object> getByActivityId(int lid) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"lid\":" + lid + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	public boolean update(Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception{
		return  mdd.update(searchMap, updateMap).isUpdateOfExisting();
	}
	
	public int getCountByProductId(String productId) throws Exception{
		return mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",50).size();
	}
	
	public int getCountByLiveId(int lid) throws Exception{
		return mdd.selectPartLimitToMap("{\"lid\":" + lid + "}",50).size();
	}
	
	public List<Map> getListByActivityId(int lid) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"lid\":" + lid + "}",1);
	    return list;
	}
	
	/**
	 * 根据直播Id获取直播商品
	 * @param lid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<String> getLiveProductsBylid(int lid) throws Exception {
		String nowDate=YMTDateUtil.getDateForMongoFormat(0,-7,0);
		ArrayList<String> list = mdd.selectPart("{'lid':" + lid+",'status':1,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}}");
		//ArrayList<String> list = liveProducts.selectPartLimitToMap("{'lid':" + lid+",'status':1,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}},"
		//		+ "{\"ispsp\":1,\"spid\":1,\"pics\":1,\"minp\":1}",productNum,"{sort:1}");

		return list;
	}
	/**
	 * 根据正在直播Id获取直播商品
	 * @param lid：直播id
	 * @param productNum:查询的商品数量 productNum=0 表示查询全部
	 * @param sort：查询排序 {sort:1,add:-1}
	 * @return
	 * @throws Exception 
	 */
	public ArrayList<Map> getLiveProductsBylid(int lid,int productNum,String sort) throws Exception {
		String nowDate=YMTDateUtil.getDateForMongoFormat(0,-9,0);
		ArrayList<Map> list;
		if(productNum>0)
		{
		 list = mdd.selectPartLimitToMap("{'lid':" + lid+",'status':1,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}}"
				,"{\"spid\":1,\"scatname\":1,\"bid\":1,\"tcatid\":1,\"scatid\":1,\"tcatname\":1,\"brand\":1}",productNum,sort);
		}
		else//全部
		{
			list = mdd.selectPartLimitToMap("{'lid':" + lid+",'status':1,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}}"
				,"{\"spid\":1,\"scatname\":1,\"bid\":1,\"tcatid\":1,\"scatid\":1,\"tcatname\":1,\"brand\":1}",99999,sort);
		}

		return list;
	}
	
	/**
	 * 根据直播Id获取直播商品(包括已结束的直播)
	 * @param lid：直播id
	 * @param productNum:查询的商品数量 productNum=0 表示查询全部
	 * @param sort：查询排序 {sort:1,add:-1}
	 * @return
	 * @throws Exception 
	 */
	public ArrayList<Map> getLiveProductsBylidAndNum(int lid,int productNum,String sort) throws Exception {
		ArrayList<Map> list;
		if(productNum>0)
		{
		 list = mdd.selectPartLimitToMap("{'lid':" + lid+",'status':1}"
				,"{\"spid\":1,\"scatname\":1,\"bid\":1,\"tcatid\":1,\"scatid\":1,\"tcatname\":1,\"brand\":1}",productNum,sort);
		}
		else//全部
		{
			list = mdd.selectPartLimitToMap("{'lid':" + lid+",'status':1}"
				,"{\"spid\":1,\"scatname\":1,\"bid\":1,\"tcatid\":1,\"scatid\":1,\"tcatname\":1,\"brand\":1}",99999,sort);
		}

		return list;
	}
	/**
	 * 根据直播Id获取直播商品
	 * @param lid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<String> getLivesProductsBySpid(String spid) throws Exception {
		ArrayList<String> list = mdd.selectPart("{\"spid\":\"" + spid + "\"}");
		return list;
	}
	public WriteResult updatetPasByUid() throws Exception {
		//WriteResult wr = liveProducts.update("{\"lid\" : " + uid + "}", "{\"tPas\" : \"54E12B6B392FD98136162FE3F7343AD5\"}");
		String nowDate=YMTDateUtil.getDateForMongoFormat(1,-7,0);
		WriteResult wr = mdd.update("{\"lid\":176551}","{\"end\" : \"{$date:'"+nowDate+"'}\"}");
		return wr;
	}
	
	public WriteResult updatePBrandByLiveAndBrank(int lid,int bid,String brand) throws Exception{
		return mdd.update("{\"lid\":"+lid+"}","{\"bid\":"+bid+",\"brand\":\""+brand+"\"}");
	}
	
	public static void main(String[] args) throws Exception {
		//f68f94f6-898a-4df7-823a-f187c0b62db3
		System.out.println(new LiveProductsWapper().getByProductIdAndLiveId("2f5983e7-e7bb-4d3e-b92a-45eae2d831d1",157416).toString());
	}
	
}